import 'package:citiesocial/_include.dart';

class SearchGoods extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _SearchGoodsState();
}

class _SearchGoodsState extends BaseState<SearchGoods> {
  final _list = List<GoodsEntity>();
  final _searchTextController = TextEditingController();
  String _keyword;
  var _page = 1;
  var _loadable = true;

  void _search() {
    if (_keyword != null && _keyword.trim().length > 0) {
      //LoadingDialog.show(context);
      HttpManager.searchGoods(_keyword, page: _page, context: context)
          .then((list) {
        //LoadingDialog.dismiss(context);
        if (_page <= 1) {
          _list.clear();
        }
        if (list != null && list.length > 0) {
          _loadable = list.length >= HttpManager.default_size;
          _list.addAll(list);
        } else {
          _loadable = false;
        }
        setState(() {});
      });
    } else {
      setState(() {
        _list.clear();
      });
    }
  }

  @override
  void dispose() {
    _searchTextController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarUtils.searchable(context,
          textController: _searchTextController, edittingComplete: () {
        _keyword = _searchTextController.text;
        _loadable = true;
        _page = 1;
        _search();
      }),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: DListView(
          onLoadmore: () {
            if (_loadable) {
              _page++;
              _search();
            }
          },
          onRefresh: () async {
            _loadable = true;
            _page = 1;
            _search();
          },
          itemCount: _list.length,
          itemBuilder: (_, index) => LongSquareGoodsItem(_list[index]),
        ),
      ),
    );
  }
}
